﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Davsha.App.Actions
{
    public class ActionInfo
    {
        private IAction _concreteAction;

        public ActionInfo()
        {
        }

        public ActionInfo(string name, string type, ActionParameterCollection parameters)
        {
            Name = name;
            Type = type;
            Parameters = parameters;
        }

        [XmlAttribute]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute]
        public string Type
        {
            get;
            set;
        }

        [XmlAttribute]
        public string Assembly
        {
            get;
            set;
        }

        [XmlArrayItem("Parameter")]
        public ActionParameterCollection Parameters
        {
            get;
            set;
        }

        [XmlIgnore]
        public IAction ConcreteAction
        {
            get
            {
                if (_concreteAction == null)
                {
                    _concreteAction = (IAction) Activator.CreateInstance(Assembly, Type).Unwrap();
                }
                return _concreteAction;
            }
        }

        public void Execute()
        {
            ConcreteAction.Execute(Parameters);
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
